﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using FwProject.Web.Shop.Models;
using FwProject.Controllers;
using Microsoft.AspNetCore.Hosting;
using Abp.AspNetCore.Mvc.Authorization;
using Microsoft.Extensions.Configuration;
using FwProject.Configuration;
using Abp.Auditing;
using Abp.Web.Models;
using FwProject.Shop;
using FwProject.Com;
using FwProject.Com.Dto;
using FwProject.Web.FwTagHelper;
using FwProject.ManagerNotification;
using Abp.Runtime.Caching;

namespace FwProject.Web.Shop.Controllers
{
    [AbpMvcAuthorize]
    public class HomeController : FwProjectControllerBase
    {
        private readonly IShopProductAppService _shopProductAppService;
        private readonly IShopOrderDetailsAppService _shopOrderDetailsAppService;
        private readonly IShopOrderAppService _shopOrderAppService;
        private readonly ICommodityCollectionAppService _commodityCollectionAppService;
        private readonly IConfigurationRoot _appConfiguration;
        private readonly SubscribeToNotifications _subscribeToNotifications;
        private readonly ICacheManager _catchmanager;

        public HomeController(IHostingEnvironment env, IShopProductAppService shopProductAppService, IShopOrderDetailsAppService shopOrderDetailsAppService, ICommodityCollectionAppService commodityCollectionAppService,
            IShopOrderAppService shopOrderAppService,
            SubscribeToNotifications subscribeToNotifications,
            ICacheManager catchmanager
            )
        {
            _catchmanager = catchmanager;
            _shopProductAppService = shopProductAppService;
            _appConfiguration = env.GetAppConfiguration();
            _shopOrderDetailsAppService = shopOrderDetailsAppService;
            _shopOrderAppService = shopOrderAppService;
            _commodityCollectionAppService = commodityCollectionAppService;
            _subscribeToNotifications = subscribeToNotifications;
            
        }
        public async Task<IActionResult> Index()
        {
            var boo = await _subscribeToNotifications.IsSubscribed_OrderNotification(null, AbpSession.UserId.Value);//是否订阅订单通知
            if (!boo)
                await _subscribeToNotifications.Subscribe_OrderNotification(null, AbpSession.UserId.Value);//订阅订单通知;

            return View();
        }
        /// <summary>
        /// 清空app缓存
        /// </summary>
        /// <returns></returns>
        public IActionResult cleanAppHomeCatch() {

            ICache cache = _catchmanager.GetCache("HomeDto");
            cache.Remove("index");
             
            return Json("success");
        }

        public IActionResult About()
        {
            ViewData["Message"] = "Your application description page.";

            return View();
        }

        public IActionResult Contact()
        {
            ViewData["Message"] = "Your contact page.";

            return View();
        }

        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public IActionResult Note()
        {
            ViewBag.ImageUrl = _appConfiguration["ImageUrl:url"].ToString();
            var model = _shopOrderDetailsAppService.GetTotalPrice();
            ViewBag.TotalPrice = model.TotalPrice;
            ViewBag.ReturnPrice = model.ReturnPrice;
            ViewBag.Price = model.Price;
            return View();
        }

        [HttpGet]
        [DisableAuditing]
        [WrapResult(false, false)]//关闭ABP的返回数据封装
        public async Task<IActionResult> GetCommodityData()
        {
            var data = await _shopProductAppService.GetNewAddCommodityAsync();
            return Json(data);
        }

        [HttpGet]
        [DisableAuditing]
        [WrapResult(false, false)]
        public async Task<IActionResult> GetStayOrders()
        {
            var data = await _shopOrderAppService.GetStayOrders();
            return Json(data);
        }
        /// <summary>
        /// 销售详细统计
        /// </summary>
        /// <returns></returns>
        public IActionResult SaleDetails()
        {
            return View();
        }

        [HttpGet]
        [DisableAuditing]
        [WrapResult(false, false)]
        public async Task<IActionResult> GetSaleDetailsData(int page, int limit)
        {
            var model = await _shopOrderDetailsAppService.GetSaleDetailsAsync(page, limit);
            return Json(model);
        }
        /// <summary>
        /// 商品销售明细
        /// </summary>
        public IActionResult ShopSaleDetails()
        {
            return View();
        }

        [HttpGet]
        [DisableAuditing]
        [WrapResult(false, false)]
        public async Task<IActionResult> GetShopSaleDetailsData(int page, int limit, string creationTime)
        {
            var model = await _shopOrderDetailsAppService.GetShopSaleDetailsAsync(page, limit, creationTime);
            return Json(model);
        }
        /// <summary>
        /// 商品收藏统计
        /// </summary>
        /// <returns></returns>
        public IActionResult ShopCollection()
        {
            return View();
        }

        [HttpGet]
        [DisableAuditing]
        [WrapResult(false, false)]
        public async Task<IActionResult> GetShopCollectionData(int page, int limit)
        {
            var model = await _commodityCollectionAppService.GetShopCollectionAsync(page, limit);
            return Json(model);
        }
        public async Task<IActionResult> GetCollectionCountData()
        {
            var list = await _commodityCollectionAppService.GetCollectionCountAsync();
            var option = new
            {
                title = new { text = "收藏商品Top10" },
                legend = new { axisTick = new { show = false }, data = new string[1] { "收藏数" } },
                yAxis = new { type = "value" },
                xAxis = new { boundaryGap = true, type = "category", axisPointer = new { type = "shadow" }, data = list.Select(o => o.ProductName).ToList() },
                series = new { name = "收藏数", type = "bar", data = list.Select(o => o.CollectionCount).ToList() }
            };
            return Json(option);
        }
        /// <summary>
        /// 商品热卖排行
        /// </summary>
        /// <returns></returns>
        public IActionResult ShopHotSelling()
        {
            return View();
        }

        [HttpGet]
        [DisableAuditing]
        [WrapResult(false, false)]
        public async Task<IActionResult> GetHotSellingCommodityData(int page, int limit)
        {
            var model = await _shopOrderDetailsAppService.GetHotSellingCommodityListAsync(page, limit);
            return Json(model);
        }

        public async Task<IActionResult> GetHotSellingReportData()
        {
            var list = await _shopOrderDetailsAppService.GetHotSellingCommodityReportAsync();

            var option = new
            {
                title = new { text = "热卖商品Top10" },
                legend = new { axisTick = new { show = false }, data = new string[1] { "下单量" } },
                yAxis = new { type = "value" },
                xAxis = new { boundaryGap = true, type = "category", axisPointer = new { type = "shadow" }, data = list.Select(o => o.ProductName).ToList() },
                series = new { name = "下单量", type = "bar", data = list.Select(o => o.OrderCount).ToList() }
            };

            return Json(option);
        }

    }
}
